
from PIL import Image
from numpy import *
from scipy.ndimage import filters
from pylab import *

sigma=3
im =array(Image.open(r'misc_pic\empire.jpg').convert('L'))

#Sobel derivative filters
imx=zeros(im.shape)
filters.gaussian_filter(im,(sigma,sigma),(0,1),imx)
#filters.sobel(im,1,imx)
imy=zeros(im.shape)
filters.gaussian_filter(im,(sigma,sigma),(1,0),imy)
#filters.sobel(im,0,imy)

magnitude=sqrt(imx**2+imy**2)

subplot(141)
gray();imshow(im);xlabel(u"原始图像")
subplot(142)
gray();imshow(imx);xlabel(u"梯度X分量")
subplot(143)
gray();imshow(imy);xlabel(u"梯度Y分量")
subplot(144)
gray();imshow(magnitude);xlabel(u"梯度幅值")
show()